﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Actions;
using COFE.Core;

namespace COFE.Data
{
    public class DBWorkScheduler : WorkScheduler
    {
        public DBWorkScheduler()
        {
        
        }

        protected override Guid enqueue(ISchedule schedule)
        {
            if (COFEShared.CurrentUserProfile == null)
                throw new Exception("Not logined.");
            COFEDBShared.DBOperations.UpdateDatabase((context) =>
            {
                if (!(context.Works.Any((w) => w.WorkId == schedule.Work.ID)))
                {
                    var dbWork = COFE.Data.Work.CreateWork(schedule.Work.ID,
                        schedule.Work.Description,
                        COFEShared.CurrentUserProfile.UserName, 
                        schedule.Work.ToString());
                    context.Works.AddObject(dbWork);
                    context.SaveChanges();
                }
            });
            return base.enqueue(schedule);
        }

        //public override Guid Enqueue(WorkPriority priority, IWork work)
        //{
            
        //}
    }
}
